package kov.core.body.sb
{
	import com.riaoo.events.AnimationEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	
	import kov.core.BodyState;
	import kov.core.IBody;
	import kov.core.StateCommand;
	import kov.core.body.StateEnum;
	import kov.core.global.Global;
	
	public class SBDa extends BodyState
	{
		[Embed("../../../../../assets/sb/da/0.gif")]
		private var Frame0:Class;
		[Embed("../../../../../assets/sb/da/1.gif")]
		private var Frame1:Class;
		[Embed("../../../../../assets/sb/da/2.gif")]
		private var Frame2:Class;
		[Embed("../../../../../assets/sb/da/3.gif")]
		private var Frame3:Class;
		[Embed("../../../../../assets/sb/da/4.gif")]
		private var Frame4:Class;
		
		public function SBDa(body:IBody, priority:int=1)
		{
			super(StateEnum.DA, body, priority);
			createRenderer();
		}
		
		// 创建渲染帧。
		private function createRenderer():void
		{
			var f0:BitmapData = (new Frame0() as Bitmap).bitmapData;
			var f1:BitmapData = (new Frame1() as Bitmap).bitmapData;
			var f2:BitmapData = (new Frame2() as Bitmap).bitmapData;
			var f3:BitmapData = (new Frame3() as Bitmap).bitmapData;
			var f4:BitmapData = (new Frame4() as Bitmap).bitmapData;
			this.renderer.push(f0);
			this.renderer.push(f1);
			this.renderer.push(f2);
			this.renderer.push(f3);
			this.renderer.push(f4);
		}
		
		override public function execute():void
		{
			this.body.renderer.frameSequence = this.renderer;
			this.body.renderer.delay = 90;
			this.body.renderer.offsetX = 81;
			this.body.renderer.offsetY = 74;
			this.body.renderer.gotoAndPlay(0);
			
			this.body.faceToLeft = this.body.x - Global.world.player.x > 0;
			
			this.body.renderer.addEventListener(AnimationEvent.ANIMATION_END, onEnd);
		}
		
		override public function exit():void
		{
			this.body.renderer.removeEventListener(AnimationEvent.ANIMATION_END, onEnd);
		}
		
		private function onEnd(event:AnimationEvent):void
		{
			this.body.executeStateCommand(new StateCommand(StateEnum.BEI_ZHAN));
		}
		
	}
}